﻿/*----------------------------------------------------------------
//Copyright (C) 2012 赵国伟
//文件功能描述：广告位置管理客户端脚本
//创建人：赵国伟
//生成时间：2012-05-12 13:28:12
//----------------------------------------------------------------*/
var AjaxServerUrl = "/Manage/AS/AdCommon.aspx";
var ModelOfAD;
var ModelOfContent;
var ADID = 0;
var SessionID;
var listOfUploader = new Array();
var hashADPic = new HashTable();

///初始化
function Init() {
    ADID = 0;
    listOfUploader = new Array();
    hashADPic = new HashTable();
}

//执行删除
function Delete(id) {
    if (!confirm("系统提示：您确定要删除该条广告位置信息？"))
        return;
    AjaxGet(AjaxServerUrl + "?Action=DeleteAD&ADID=" + id);
}

///检查表单；
function CheckForm() {
    if ($("#txtADName").val() == "") {
        alert("系统提示：请输入广告位置名称！");
        $("#txtADName").focus();
        return false;
    }
    if ($("#txtAdWidth").val() == "") {
        alert("系统提示：请输入广告宽度！");
        $("#txtAdWidth").focus();
        return false;
    }
    if (!IsNumber($("#txtAdWidth").val()) || parseInt($("#txtAdWidth").val()) <= 0) {
        alert("系统提示：广告宽度必须为数字并且大于0！");
        $("#txtAdWidth").focus();
        return false;
    }
    if ($("#txtAdHeight").val() == "") {
        alert("系统提示：请输入广告高度！");
        $("#txtAdHeight").focus();
        return false;
    }
    if (!IsNumber($("#txtAdHeight").val()) || parseInt($("#txtAdHeight").val()) <= 0) {
        alert("系统提示：广告高度必须为数字并且大于0！");
        $("#txtAdHeight").focus();
        return false;
    }
    if ($("#ddlAdNums").val() == "") {
        alert("系统提示：请设置广告数量！");
        $("#ddlAdNums").focus();
        return false;
    }
    var counts = parseInt($("#ddlAdNums").val());
    for (var i = 0; i < counts; ++i) {
        var bianhao = i + 1;
        if ($("#txtADPicUrl" + bianhao).val() == "") {
            alert("系统提示：请上传广告" + bianhao + "的图片！");
            $("#txtADPicUrl" + bianhao).focus();
            return false;
        }
    }
    return true;
}

//保存；
function Save() {
    $("#btnSave").attr("disabled", true);
    if (!CheckForm()) {
        $("#btnSave").attr("disabled", false);
        return;
    }

    var data = clone(ModelOfAD);
    data.AD._adid = ADID;
    data.AD._adname = $("#txtADName").val();
    data.AD._adwidth = $("#txtAdWidth").val();
    data.AD._adheight = $("#txtAdHeight").val();
    data.AD._adnums = $("#ddlAdNums").val();
    data.AD._createdate = "/Date(" + CurrentDateStringToUTC() + ")/";
    data.AD._lastmodifydate = "/Date(" + CurrentDateStringToUTC() + ")/";

    data.Contents = new Array();
    var counts = parseInt($("#ddlAdNums").val());
    for (var i = 0; i < counts; ++i) {
        var bianhao = i + 1;
        var c = clone(ModelOfContent);
        c._adid = ADID;
        c._adlinkurl = $("#txtAdLinkUrl"+bianhao).val();
        c._adpicurl = $("#txtADPicUrl" + bianhao).val();
        data.Contents.push(c);
    }

    var url = AjaxServerUrl + "?Action=Save";
    AjaxPost(url, "btnSave", data);  //更新
}

//绑定广告位置信息到录入表单中；
function BindADToForm() {
    var url = AjaxServerUrl + "?action=GetAD&ADID=" + ADID + "&tttt=" + Math.random();
    $.ajax(
    {
        type: "Get",
        url: url,
        dataType: "json",
        success: function (data) {
            if (data != null) {
                $("#txtADName").val(data.AD._adname);
                $("#txtAdWidth").val(data.AD._adwidth);
                $("#txtAdHeight").val(data.AD._adheight);
                $("#ddlAdNums").val(data.AD._adnums);

                if (data.Contents != null) {
                    for (var i = 0; i < data.Contents.length; ++i) {
                        var bianhao = i + 1;
                        $("#AdLinkUrl" + bianhao).val(data.Contents[i]._adlinkurl);
                        $("#ADPicUrl" + bianhao).val(data.Contents[i]._adpicurl);
                        hashADPic.set(bianhao, data.Contents[i]._adpicurl);
                    }
                }
                GenerateUploadTable(data.AD._adnums);
            }
        }
    });
}

///添加界面；
function Add() {
    Init();
    GetPopupDiv(900, 800, "#e2e2e2", "添加广告位置", GetHtmlForm());
}

//编辑界面；
function Edit(id) {
    Init();
    ADID = id;
    GetPopupDiv(900, 800, "#e2e2e2", "编辑广告位置", GetHtmlForm());
    BindADToForm();
}


function uploadSuccess(file, fileUrl) {
    try {
        $("#" + this.customSettings.upload_target).html("<a href=\"" + fileUrl + "\" title=\"查看\" target=\"_blank\"><img src=\"" + fileUrl + "\"></a>");
        var bianhao = this.customSettings.upload_target.replace("spUploadPic", "");
        hashADPic.set(bianhao, fileUrl);
        var hdTxt = "#txtADPicUrl" + bianhao;
        $(hdTxt).val(fileUrl);
    } catch (ex) {
        this.debug(ex);
    }
}



function GenerateUploadTable(paramNums) {
    $("#trAD").hide();
    if (paramNums == "") {
        $("#spAD").html("");
        return;
    }
    for (var i = 0; i < listOfUploader.length; ++i) {
       listOfUploader[i].destroy();
    }
    listOfUploader = new Array();

    var counts = parseInt(paramNums);
    var html = new Array();
    html.push("    <table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
    for (var i = 1; i <= counts; ++i) {
        html.push("      <tr >");
        html.push("        <td width=\"100\"  align=\"right\" >广告<span class=\"blue bold\">"+i+"</span>链接地址：</td>");
        html.push("        <td  ><input name=\"txtAdLinkUrl" + i + "\" value=\"http://\" size=\"80\" type=\"text\" id=\"txtAdLinkUrl" + i + "\" class=\"border6\" />");
        html.push("          (*)</td>");
        html.push("        </tr>");
        html.push("      <tr>");
        html.push("        <td rowspan=\"2\" align=\"right\">广告<span class=\"blue bold\">" + i + "</span>图片地址：</td>");
        html.push("        <td ><span id=\"spUploadBtn"+i+"\"></span>&nbsp;</td>");
        html.push("     </tr>");

        var imgurl = hashADPic.items2(i);
        var imgContent = imgurl != "" ? "<a href=\"" + imgurl + "\"><img src=\"" + imgurl + "\" ></a>" : "";
        html.push("     <tr id=\"trUploadPic"+i+"\" >");
        html.push("        <td ><span id=\"spUploadPic" + i + "\">" + imgContent + "</span><input name=\"txtADPicUrl" + i + "\" id=\"txtADPicUrl" + i + "\" type=\"hidden\" value=\"" + imgurl + "\"></td>");
        html.push("     </tr>");
    }
    html.push("    </table>");
    $("#spAD").html(html.join(""));
    for (var i = 1; i <= counts; ++i) {
        var swf = CreateSwfUploadObject("/Manage/AS/AjaxUpload.aspx?action=UploadFiles", SessionID, "*.jpg;*.gif;*.png", "图片文件", "spUploadBtn" + i, "spUploadPic" + i);
        listOfUploader.push(swf);
    }
    $("#trAD").show();
}


///获取录入界面的HTML;
function GetHtmlForm() {
    var html = new Array();
    html.push("<form>");
    html.push("<table width=\"900\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"table\">");
    html.push("  <tr>");
    html.push("    <td colspan=\"2\" class=\"TableHeadRow\">&gt;&gt;广告基本信息</td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td width=\"120\" align=\"right\">广告位置：</td>");
    html.push("    <td ><input name=\"txtADName\" type=\"text\"  class=\"border6\" id=\"txtADName\" size=\"40\">");
    html.push("    <span class=\"red\">(*)</span></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">广告宽度：</td>");
    html.push("    <td><input name=\"txtAdWidth\" type=\"text\" class=\"border6\" id=\"txtAdWidth\" value=\"0\" size=\"5\" />");
    html.push("      <span class=\"red\">(*)</span></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">广告高度：</td>");
    html.push("    <td><input name=\"txtAdHeight\" type=\"text\" class=\"border6\" id=\"txtAdHeight\" value=\"0\" size=\"5\" />");
    html.push("      <span class=\"red\">(*)</span></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">广告数量：</td>");
    html.push("    <td><select name=\"ddlAdNums\" id=\"ddlAdNums\" onchange=\"GenerateUploadTable(this.options[this.selectedIndex].value)\">");
    html.push("      <option value=\"\" >请选择广告数量</option>");
    for (var i = 1; i <= 5; ++i) {
        html.push("      <option value=\"" + i + "\">" + i + "个</option>");
    }
    html.push("            </select></td>");
    html.push("  </tr>");
    html.push("  <tr id=\"trAD\" style=\"display:none;\">");
    html.push("    <td colspan=\"2\" ><span id=\"spAD\">");
    html.push("</span></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">&nbsp;</td>");
    html.push("    <td><input name=\"btnSave\" type=\"button\" id=\"btnSave\" value=\" 保 存 \" class=\"btn2\" onclick=\"Save();\">");
    html.push("    <input name=\"btnReset\" type=\"reset\" id=\"btnReset\" value=\" 重 置 \" class=\"btn2\">");
    html.push("    <input name=\"btnClose\" type=\"button\" id=\"btnClose\" value=\"关闭本窗口\" class=\"btn2\" onclick=\"yesOther(false);\"></td>");
    html.push("  </tr>");
    html.push("</table>");
    html.push("</form>");

    return html.join("");

}







function yesOther(t) {
    if (listOfUploader != null) {
        for (var i = 0; i < listOfUploader.length; ++i) {
            listOfUploader[i].destroy();
        }
    }
    var IsHavePopDiv = false;
    var lastDivName = "";
    var divs = document.getElementsByTagName("DIV");
    for (var i = 0; i < divs.length; i++) {
        if (divs[i].id.indexOf(prePopDivName) >= 0) {
            fullPopDivName = divs[i].id;
        }
    }
    for (var i = 0; i < divs.length; i++) {
        if (divs[i].id.indexOf(prePopDivName) >= 0 && divs[i].id != fullPopDivName) {
            oo(divs[i].id).style.display = "";
            IsHavePopDiv = true;
        }
    }
    if (!t) {
        var bgObj2 = document.getElementById(fullPopDivName);
        document.body.removeChild(bgObj2);
        if (!IsHavePopDiv) {
            var bgObj = document.getElementById("bgDiv");
            document.body.removeChild(bgObj);
        }
    }
    else {
        var bgObj2 = document.getElementById(fullPopDivName);
        document.body.removeChild(bgObj2);
        if (!IsHavePopDiv) {
            var bgObj = document.getElementById("bgDiv");
            document.body.removeChild(bgObj);
        }
    }
    for (var i = 0; i < divs.length; i++) {
        if (divs[i].id.indexOf(prePopDivName) >= 0) {
            fullPopDivName = divs[i].id;
        }
    }
}







